package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 存储喜欢数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        // 1.校验参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null || apUser.getId().equals(0)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 2.根据用户id和作者id查询redis
        String likeKey = "USER_LIKES" + dto.getArticleId();
        Boolean likeFlag = redisTemplate.opsForHash().hasKey(likeKey, apUser.getId().toString());
        // 3.如果已点赞，并且继续要点赞该文章，则终止
        if (likeFlag && dto.getOperation() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "已点赞该文章，无法继续点赞");
        }
        // 4.如果已点赞，并且要取消点赞该文章，则删除redis和mongo
        if (likeFlag && dto.getOperation() == 1) {
            redisTemplate.opsForHash().delete(likeKey, apUser.getId().toString());
            mongoTemplate.remove(Query.query(
                    Criteria.where("userId").is(apUser.getId())
                            .and("articleId").is(dto.getArticleId())
            ), ApLikesBehavior.class);
        }
        // 5.如果没点赞，并且要取消点赞，则终止
        if (!likeFlag && dto.getOperation() == 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "没点赞该文章，无法取消点赞");
        }
        // 6.如果没关注，并且要关注该作者，则新增redis和mongo
        if (!likeFlag && dto.getOperation() == 0) {
            redisTemplate.opsForHash().put(likeKey, apUser.getId().toString(), "1");
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setUserId(Long.valueOf(apUser.getId()));
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior);
        }
        return ResponseResult.okResult(200, "点赞文章成功");
    }
}
